//
//  RZQPartLongViewController.m
//  LastOne
//
//  Created by dllo on 15/11/11.
//  Copyright © 2015年 sc. All rights reserved.
//

#import "RZQPartLongViewController.h"
#import "RZQDataCollectionViewCell.h"
#import "FitHeader.h"
#import "RZQFindWebViewController.h"
#import <MJRefresh.h>
#import "HTTPMethod.h"
#import "NightHeader.h"
#import "GiFHUD.h"
@interface RZQPartLongViewController ()<UICollectionViewDelegate, UICollectionViewDataSource, UICollectionViewDelegateFlowLayout>
@property(nonatomic,  strong)UICollectionView *collectionView;
@property(nonatomic, strong)NSMutableArray *dataSource;
@property(nonatomic, strong)NSNumber *page;
@end

@implementation RZQPartLongViewController
-(void)viewWillAppear:(BOOL)animated
{
    self.navigationController.navigationBar.hidden = NO;
}
#pragma mark 网络请求
-(void)getNet
{
    [GiFHUD show];
    
    [HTTPMethod getDataByString:[NSString stringWithFormat:@"http://app.qdaily.com/app/categories/index/%ld/%@.json", _index,_page] BodyDic:nil WithDataBlock:^(id data) {
        NSDictionary *dic = [data objectForKey:@"response"];
        NSDictionary *dic1 = [dic objectForKey:@"feeds"];
        _page = [dic1 objectForKey:@"last_time"];
        NSArray *array = [dic1 objectForKey:@"list"];
        
        for (NSDictionary *Dic in array) {
            CirleModel *cirle  = [[CirleModel alloc]init];
            NSDictionary *postDic = [Dic objectForKey:@"post"];
            
            [cirle setValuesForKeysWithDictionary:postDic];
            cirle.image = [Dic objectForKey:@"image"];
            cirle.type = [Dic objectForKey:@"type"];
            [_dataSource addObject:cirle];
        }
        [_collectionView reloadData];
        
        //停止刷新
        [_collectionView.header endRefreshing];
        [_collectionView.footer endRefreshing];
        [GiFHUD dismiss];
    }];
}
- (void)viewDidLoad {
    [super viewDidLoad];
    _page = [NSNumber numberWithInt:0];
    [self getNet];
    self.view.backgroundColor = [UIColor whiteColor];
    UICollectionViewFlowLayout *dataFlowLayout = [[UICollectionViewFlowLayout alloc]init];
    dataFlowLayout.minimumInteritemSpacing = 5 ;
    dataFlowLayout.minimumLineSpacing = 5;
    dataFlowLayout.sectionInset = UIEdgeInsetsMake(5 , 5 , 5, 5 );
    
    _collectionView = [[UICollectionView alloc]initWithFrame:CGRectMake(0, 0, ScreenWidth, ScreenHeight - 113 * FitHeight) collectionViewLayout:dataFlowLayout];
    _collectionView.delegate = self;
    _collectionView.dataSource = self;
    _collectionView.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:_collectionView];
     [GiFHUD setGifWithImageName:@"loading.gif"];
    _dataSource = [NSMutableArray array];
    [_collectionView registerClass:[RZQDataCollectionViewCell class] forCellWithReuseIdentifier:@"rzqIdentifier"];
    [_collectionView NightWithType:UIViewColorTypeNormal];
    _collectionView.header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        _page = [NSNumber numberWithInt:0];
        [_dataSource removeAllObjects];
        [self getNet];
        
        
    }];
    _collectionView.footer = [MJRefreshAutoNormalFooter footerWithRefreshingBlock:^{
        
        [self getNet];
        
    } ];
    
    
    
    
    
    
    // Do any additional setup after loading the view.
}
-(CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
{
    
    
    return  CGSizeMake(ScreenWidth / 2 - 10 * FitWidth  , 200 * FitHeight);
    
    
}
-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    RZQDataCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"rzqIdentifier" forIndexPath:indexPath];
    cell.cirData = _dataSource[indexPath.row];
    return cell;
}
-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return _dataSource.count;
}
-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    self.hidesBottomBarWhenPushed = YES;
    self.navigationController.navigationBar.hidden = YES;
    RZQFindWebViewController *rzq = [[RZQFindWebViewController alloc]init];
    [self.navigationController pushViewController:rzq animated:YES];
    rzq.modelData = _dataSource[indexPath.row];
    rzq.flag = _index;
    
    self.hidesBottomBarWhenPushed = NO;
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
